﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using pwblog.Entities;
using pwblog.BusinessObject;

namespace pwblog.Web
{
    public partial class Information : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // hien thi ten cua nguoi dung
            lblName.Text = User.Identity.Name;

            if (!Page.IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            Account[] account = new AccountBo().GetAccountByUserName(User.Identity.Name);
            if (account != null)
            {
                dvAccount.DataSource = account;
                dvAccount.DataBind();
            }
        }

        protected void dvAccount_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            string birthday = ((TextBox)dvAccount.FindControl("tbBirthday")).Text;
            string address = ((TextBox)dvAccount.FindControl("tbAddress")).Text;
            if (new AccountBo().UpdateAccount(User.Identity.Name, birthday, address))
            {
                // thanh coong
            }
            else
            {
                // that bai
            }
            dvAccount.ChangeMode(DetailsViewMode.ReadOnly);
            LoadData();
        }

        protected void dvAccount_ModeChanging(object sender, DetailsViewModeEventArgs e)
        {
            if (e.CancelingEdit == true)
            {
                dvAccount.ChangeMode(DetailsViewMode.ReadOnly);
                LoadData();
            }
        }

        protected void dvAccount_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                dvAccount.ChangeMode(DetailsViewMode.Edit);
                LoadData();
            }

            if (e.CommandName == "Select")
            {
                Response.Redirect(@"ChangePassword.aspx");
            }
        }
    }
}